package am.aua.renju.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "MainServlet", urlPatterns = {"/index"})
public class MainServlet extends HttpServlet {

    private Controller controller;

    @Override
    public void init() {
        controller = new Controller(getServletContext());
    }   

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String requestParam = request.getParameter("request");
        request.getSession();

        if (requestParam != null) {
            if (requestParam.equals("loggedin")) {
                controller.loggedin(request, response);
            } else if (requestParam.equals("registred")) {
                controller.registered(request, response);
            } else if (requestParam.equals("deleteGame")) {
                controller.deleteGame(request, response);
            } else if (requestParam.equals("logout")) {
                controller.logout(request, response);
            } else if (requestParam.equals("createGame")) {
                controller.createGame(request, response);
            } else if (requestParam.equals("createdGame")) {
                controller.createdGame(request, response);
            } else if (requestParam.equals("loggedinGameData")) {
                controller.loggedinGameData(request, response);
            } else if (requestParam.equals("joinGame")) {
                controller.joinGame(request, response);
            } else if (requestParam.equals("opponentPlay")) {
                controller.opponentPlay(request, response);
            } else if (requestParam.equals("play")) {
                controller.play(request, response);
            } else if (requestParam.equals("joinRequest")) {
                controller.joinRequest(request, response);
            } else if (requestParam.equals("top10Players")) {
                controller.top10Players(request, response);
            } else if (requestParam.equals("joinGameHost")) {
                controller.joinGameHost(request, response);
            } else if (requestParam.equals("isMyTurn")) {
                controller.isMyTurn(request, response);
            } else {
                controller.sessionDriven(request, response);
            }
        } else {            
            controller.sessionDriven(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
